package com.ken.zeratul.auto.config.dlock;

import com.ken.zeratul.dlock.aspect.DLockAspect;
import com.ken.zeratul.dlock.lock.redisson.RedissonDLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 分布式锁的自动装配
 */
@Configuration
@Slf4j
public class DLockAutoConfiguration {

    /**
     * 加载分布式锁的切面
     * @return
     */
    @Bean
    public DLockAspect getAspect(){
        log.info("【分布式锁】分布式锁增强加载...");
        return new DLockAspect();
    }

    /**
     * 基于Redis的分布式锁实现
     */
    @Bean
    @Primary
    @ConditionalOnClass(name = "org.redisson.api.RedissonClient")
    public RedissonDLock getRedissonDLock(){
        log.info("【分布式锁】基于Redis的分布式锁实现加载...");
        return new RedissonDLock();
    }
}
